package com.derbysoft.synxisadapter.dswitch3;

import com.derbysoft.dswitch.dto.hotel.common.TransactionActionType;
import com.derbysoft.dswitch.remote.hotel.dto.*;
import com.derbysoft.synxisadapter.commons.log.LogUtils;
import com.derbysoft.synxisadapter.commons.log.PerformanceLogger;
import com.derbysoft.synxisadapter.commons.repository.ReservationRepository;
import com.derbysoft.synxisadapter.commons.repository.TransactionLogRepository;
import com.derbysoft.synxisadapter.dswitch3.handler.RemoteServiceHandler;
import com.derbysoft.synxisadapter.dswitch3.service.impl.BaseHotelRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executor;

import static com.derbysoft.synxisadapter.dswitch3.PerformanceHelper.*;

/**
 * Created by: jason
 * Date: 2012-05-15
 */
@Service("ds3hotelProviderWebService")
public class HotelRemoteServiceProxy extends BaseHotelRemoteService {

    @Autowired
    private RemoteServiceHandler<HotelAvailRequest, HotelAvailResponse> ds3AvailabilityHandler;

    @Autowired
    private RemoteServiceHandler<HotelReservationRequest, HotelReservationResponse> ds3PreviewHandler;

    @Autowired
    private RemoteServiceHandler<HotelReservationRequest, HotelReservationResponse> ds3ReservationHandler;

    @Autowired
    private RemoteServiceHandler<HotelCancelRequest, HotelCancelResponse> ds3CancelHandler;

    @Autowired
    @Qualifier("taskExecutor")
    private Executor executor;

    @Autowired
    private TransactionLogRepository transactionLogRepository;

    @Autowired
    private ReservationRepository reservationRepository;

    @Override
    public HotelAvailResponse getNoCachedAvailability(HotelAvailRequest request) {
        return getAvailability(request);
    }

    @Override
    public HotelAvailResponse getAvailability(HotelAvailRequest request) {
        PerformanceLogger.init();
        HotelAvailResponse response = ds3AvailabilityHandler.handle(request);

        saveAvailPerformanceLog(executor, request, response);

        saveTransactionLog();
        return response;
    }

    @Override
    public HotelReservationResponse makeReservation(HotelReservationRequest request) {
        PerformanceLogger.init();
        HotelReservationResponse response;
        if (request.getHotelReservationRQ().getResActionType() == TransactionActionType.INITIATE) {
            response = ds3PreviewHandler.handle(request);
        } else {
            response = ds3ReservationHandler.handle(request);
        }
        saveReservationPerformanceLog(executor, request, response);

        saveReservationLog();
        return response;
    }

    @Override
    public HotelCancelResponse cancelReservation(HotelCancelRequest request) {
        HotelCancelResponse response = ds3CancelHandler.handle(request);
        saveCancellationPerformanceLog(executor, request, response);
        saveReservationLog();
        return response;
    }

    private void saveTransactionLog() {
        LogUtils.saveAvailLogs(executor, transactionLogRepository);
    }

    private void saveReservationLog() {
        LogUtils.saveReservationLogs(executor, transactionLogRepository,reservationRepository);
    }
}
